package nc.bs.arap.selectedpay;

import java.util.Vector;

import nc.bs.arap.pub.CreatJoinSQLTool;
import nc.bs.arap.pub.PubMethods;
import nc.bs.framework.exception.ComponentException;
import nc.bs.framework.server.util.NewObjectService;
import nc.vo.arap.exception.ExceptionHandler;
import nc.vo.arap.pub.NewPubMethods;
import nc.vo.arap.pub.QryCondArrayVO;
import nc.vo.arap.pub.VOUtil;
import nc.vo.arap.selectedpay.ARAPValueObject;
import nc.vo.arap.selectedpay.AggreateVO;
import nc.vo.arap.selectedpay.DMO;
import nc.vo.ep.dj.DJZBHeaderVO;
import nc.vo.ep.dj.DJZBItemVO;
import nc.vo.pub.BusinessException;


public class BillFilterBO {
	public BillFilterBO() {
		super();
	}


	public Vector getVOBySQL(int startindex, int count, Object[] vo, String fromThird, String whereThird, String pk_corp, String pk_user) throws BusinessException {
		
		
		Vector result = new Vector();
		String tempsql = PubMethods.getBillQuerySubSql1((QryCondArrayVO[]) vo[0], (nc.vo.pub.query.ConditionVO[]) vo[1], pk_corp, pk_user);
		int indexfrom = tempsql.indexOf("from");
		int indexwhere = tempsql.indexOf("where");
		String from = tempsql.substring(indexfrom, indexwhere);
		String where = tempsql.substring(indexwhere);
		int index = where.lastIndexOf(")");
		where = where.substring(0, index);
		StringBuffer sql = new StringBuffer();
		sql.append("select zb.vouchid,fb.fb_oid ");
		sql.append(from);
		if (vo[0] != null && ((QryCondArrayVO[]) vo[0]).length != 0) {
			Vector<Object> temp = new Vector<Object>();
			temp.add(vo[0]);
			String[] tablename = NewPubMethods.getInstance().getCustCondTabName(temp, null);
			try {
				fromThird = fromThird + " " + new CreatJoinSQLTool().getJoinSQL(tablename) + " ";
			} catch (Exception e) {
				throw ExceptionHandler.handleException(this.getClass(),e);

			}
		}
		sql.append(fromThird);
		sql.append(" " + where + whereThird);
		sql.append(" order by zb.vouchid");
		String sql1 = sql.toString();
		try {

			BillFilterDMO dmo = new BillFilterDMO();
			Vector<Vector<String>> ids = dmo.getIDs(startindex, count, sql1);
			
			DJZBHeaderDMO headerdmo = new DJZBHeaderDMO();
			DJZBItemDMO itemdmo = new DJZBItemDMO();
			Vector<String> headerid = new Vector<String>();
			Vector<String> itemid = new Vector<String>();
			for (int i = 0; i < ids.size(); i++) {
				Vector<String> sec = ids.get(i);
				headerid.add(sec.get(0));
				for (int k = 1; k < sec.size(); k++) {
					itemid.add(sec.get(k));
				}
			}
			
			Vector headers = null;
			String[] tempheader = new String[headerid.size()];
			headerid.copyInto(tempheader);

			headers = headerdmo.findByPrimaryKeys(tempheader);
			DJZBHeaderVO[] zbheader = new DJZBHeaderVO[headers.size()];
			headers.copyInto(zbheader);
			


			DJZBItemVO[] items = null;
			String[] tempitem = new String[itemid.size()];
			itemid.copyInto(tempitem);
			items = (DJZBItemVO[]) itemdmo.findByPrimaryKeys(tempitem);

			
			result = VOUtil.match(zbheader, items);
			
		} catch (Exception e) {
			ExceptionHandler.handleException(this.getClass(), e);
		}

		return result;
	}

	public Vector getVOsBySQL(String voname, String pk_corp, String wheresql) throws BusinessException {
		Vector result = null;
		try {
			result = new BillFilterDMO().getARAPVO(voname, pk_corp, wheresql);
		} catch (Exception e) {
			ExceptionHandler.handleException(this.getClass(), e);
		}
		return result;
	}

	public Vector getZBVOBySQL(int startindex, int count, Object[] vo, String pk_corp, String pk_user, String voname) throws BusinessException {
		AggreateVO arapvo = null;
		try {
			try {
				arapvo = (AggreateVO)NewObjectService.newInstance("arap",voname);
			} catch (ComponentException ex) {

				throw ex;

			}

		} catch (Exception e) {
			ExceptionHandler.handleException(this.getClass(), e);			
		}
		
		Vector result = null;
		String a = PubMethods.getCustomWhereSQL((nc.vo.pub.query.ConditionVO[]) vo[1]);
		if (a == null || a.length() == 0) {
			a = "";
		} else {
			a = " and " + a;
		}
		String tempsql = (String) PubMethods.getWhereSQL((QryCondArrayVO[]) vo[0], null, null)[0] + a;
		StringBuffer sql = new StringBuffer();
		sql.append("select zb." + ((ARAPValueObject) arapvo.createParentVO()).getPKName() + " ");
		sql.append("from " + ((ARAPValueObject) arapvo.createParentVO()).getTableName() + " zb");
		sql.append(" where " + tempsql);
		sql.append(" order by zb." + ((ARAPValueObject) arapvo.createParentVO()).getPKName());

		String sql1 = sql.toString();
		ExceptionHandler.debug("BillFilterBo::sql: " + sql1);
		try {
			BillFilterDMO dmo = new BillFilterDMO();
			Vector ids = dmo.getZBIDs(startindex, count, sql1);
			if (ids == null || ids.size() == 0) {
				return result;
			}

			DMO dmo1;
			try {				
				dmo1 = (DMO) NewObjectService.newInstance("arap",((ARAPValueObject) arapvo.createParentVO()).getDmo());
			} catch (ComponentException ex) {
				throw ex;

			}


			String[] keys = new String[ids.size()];
			ids.copyInto(keys);
			result = dmo1.getByPrimaryKeys(keys, pk_corp);

		} catch (Exception e) {
			ExceptionHandler.handleException(this.getClass(), e);
		}
		return result;
	}

}
